package org.example.mydeom.security;



import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.mydeom.util.ResultUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        ResultUtil.Respond(
                request.getAttribute("error")==null ? "未登录":request.getAttribute("error").toString()
                ,authException.getMessage()
                ,401,
                response
        );
//        Result<Object> result = new Result<>();
//        result.setCode(0);
//        result.setMsg("未登录");
//        result.setData(authException.getMessage());
//        String jsonString = JSON.toJSONString(result);
//        response.setContentType("application/json;charset=UTF-8");
//        response.getWriter().write(jsonString);
    }
}
